package com.shunbo.convenient.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shunbo.convenient.service.AreaService;
import com.shunbo.convenient.service.StoreService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.utils.MapUtil;
import com.shunbo.convenient.utils.Results;
import com.shunbo.convenient.vo.Area;
import com.shunbo.convenient.vo.Store;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.shunbo.convenient.utils.MapUtil.MapLnglat;

/**
 * @author weizhuang
 * 小区
 */
@RestController
@RequestMapping("/area")
public class AreaController {

    @Resource
    private AreaService areaService;
    @Resource
    private StoreService storeService;

    /**
     * 小区列表
     *
     * @param area 小区vo
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getAddressList(@RequestBody Area area) {
        try {
            PageHelper.startPage(area.getPageNo(), 10);
            List<Area> all = areaService.findAll(area);
            PageInfo<Area> pageInfo = new PageInfo<>(all);
            Map<String, Object> map = new HashMap<>();
            map.put("areaList", all);
            map.put("pageNo", area.getPageNo());
            map.put("count", pageInfo.getTotal());
            return ConvenientResult.build(200, "成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 小区列表(不分页)
     *
     * @param area 小区vo
     * @return Result结果集
     */
    @RequestMapping("/listNoPage")
    public ConvenientResult getAddressListNoPage(@RequestBody Area area) {
        try {
            List<Area> all = areaService.findAll(area);
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


    /**
     * 根据经纬度 获取区
     *
     * @param lat  经纬度
     * @param lng  经纬度
     * @return Result结果集
     */
    @RequestMapping("/getDistrict")
    public ConvenientResult getDistrict(String lat ,String lng){
        Results results = MapLnglat(lat, lng);
        return ConvenientResult.build(200, "成功", results.getAddressComponent().getDistrict());
    }

    /**
     * 小区列表
     *
     * @param area 小区vo
     * @return Result结果集
     */
    @RequestMapping("/listStore")
    public ConvenientResult getlistStore(@RequestBody Area area) {
        try {
            List<Store> storeList = new ArrayList<>();
            List<Area> all = areaService.findAll(area);
            for (Area are : all
            ) {
                double distance = MapUtil.Distance(area.getLngs(), area.getLats(), Double.parseDouble(are.getLng()), Double.parseDouble(are.getLat()));
                are.setJuli(distance + "");
                Store store = new Store();
                store.setAreaId(are.getId() + "");
                List<Store> all1 = storeService.findAll(store);
                for (Store stroe: all1
                     ) {
                    stroe.setJuli(distance+"");
                }
                storeList.addAll(all1);

            }
            return ConvenientResult.build(200, "成功", storeList);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新小区
     *
     * @param area 小区vo
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateAddress(@RequestBody Area area) {
        try {
            Integer save = areaService.update(area);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 添加小区
     *
     * @param area 小区vo
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveAddress(@RequestBody Area area) {
        try {
            Integer save = areaService.save(area);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


}
